var UserTutorCity = app.controller('UserTutorCity', function($http, $scope, $rootScope, $location,$cordovaDevice,$cordovaChat,anchorScroll,$cordovaGeolocation,UserTutorCityData) {
	try{
		$rootScope.showf=false;
		$scope.list={};
		$scope.s='';
		$scope.location="";
		$scope.p='';
		$scope.cp=false;
		$scope.busy=false;
		
		var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=cityp"+"&s="+$scope.s;
		$http.jsonp(url).success(function(data) {
			$scope.plist=data.retval;
		});
		
		var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=city"+"&s="+$scope.s;
		$http.jsonp(url).success(function(data) {
			
			$scope.list=data.retval;
		});
		
		$scope.sgo=function(id){
			$scope.p=id;
			anchorScroll.toView('#'+id, true);
			$scope.cp=true;
			setTimeout(function(){
				$scope.cp=false;
				$scope.$apply();
			},1000);
		}
		
		//获取当前位置
		$scope.getposition = function() {
			try{
				$scope.busy=true;
				var url=SITE_URL+"index.php?app=baseapp&act=none&callback=JSON_CALLBACK";
				$http.jsonp(url).success( function(data) {
					if(data.done){
						if(window.device&&window.device.platform=='Android'){

							baidu_location.getCurrentPosition(function(p){

								console.log('baidu_location.getCurrentPosition',p);
								p=JSON.parse(p);

								$rootScope.position="我的位置";

								window.localStorage.lat  = p.latitude;
								window.localStorage.lng =  p.lontitude;

								url="http://api.map.baidu.com/geocoder/v2/?ak=kkA94xvGPjnprY92WU4yYME8&callback=JSON_CALLBACK&location="+window.localStorage.lat+","+window.localStorage.lng+"&output=json&pois=0";
								$http.jsonp(url).success( function(data) {

									$scope.l = data.result.addressComponent; 

									if($rootScope.position=="我的位置"&&$scope.l.city!=undefined&&$scope.l.city!=""){
										$rootScope.position=$scope.l.city+$scope.l.district+$scope.l.street;
										url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=citycompare"+"&s="+$scope.l.city;
										$http.jsonp(url).success(function(data) {
											$scope.busy=false;
											$scope.location=data.retval;
										});
									}
									
								});

							}, function(err){
								console.log(err);
								if($scope.list!=null&&($rootScope.btrs1_city==undefined||$rootScope.btrs1_city==""||$rootScope.btrs1_city==null)){
									$scope.location=$scope.list[0]['item'][0].region_name;
								}
								$scope.busy=false;
							})
						}else{
							$scope.busy=true;
							var posOptions = {timeout: 60000, enableHighAccuracy: false};
							$cordovaGeolocation.getCurrentPosition(posOptions).then(function (position) {
								$rootScope.ajaxloading=false;
								var url="http://api.map.baidu.com/geoconv/v1/?coords="+position.coords.longitude+","+position.coords.latitude+"&from=1&to=5&ak=kkA94xvGPjnprY92WU4yYME8&callback=JSON_CALLBACK"
								$http.jsonp(url).success( function(data) {
									if(data.result.length>0){

										window.localStorage.lat  = data.result[0].y;
										window.localStorage.lng =  data.result[0].x;

										$rootScope.position="我的位置";

										url="http://api.map.baidu.com/geocoder/v2/?ak=kkA94xvGPjnprY92WU4yYME8&callback=JSON_CALLBACK&location="+window.localStorage.lat+","+window.localStorage.lng+"&output=json&pois=0";
										$http.jsonp(url).success( function(data) {
											
											$scope.l = data.result.addressComponent; 

											if($rootScope.position=="我的位置"&&$scope.l.city!=undefined&&$scope.l.city!=""){
												$rootScope.position=$scope.l.city+$scope.l.district+$scope.l.street;
												
												url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=citycompare"+"&s="+$scope.l.city;
												$http.jsonp(url).success(function(data) {
													$scope.busy=false;
													$scope.location=data.retval;
												});
											}
										});

									}

								}); 
							}, function(err) {
								$scope.busy=false;
								console.log(err);
								if($scope.list!=null&&($rootScope.btrs1_city==undefined||$rootScope.btrs1_city==""||$rootScope.btrs1_city==null)){
									$scope.location=$scope.list[0]['item'][0].region_name;
								}
							});
						}
					}
				}); 
			}
			catch(e){
				showtoast('定位失败');
				showcatch(e);
			}
		};
		
		if($rootScope.btrs1_city!=undefined&&$rootScope.btrs1_city!=""&&$rootScope.btrs1_city!=null){
			$scope.location=$rootScope.btrs1_city;
		}
		else{
			$scope.busy=true;
			$scope.getposition();
		}
		
		//位置点击
		$scope.locationclick=function(){
			$rootScope.btrs1_city=$scope.location;
			$rootScope.back();
		}
		
		//搜索
		$scope.search=function(){
			$("#search").blur();
			if ($scope.busy) {
				return false;
			}
			$scope.busy = true;
			var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=city"+"&s="+$scope.s;
			$http.jsonp(url).success(function(data) {
				$scope.list=data.retval;
				$scope.busy = false;
			});
		}
		
		//分类点击
		$scope.click1=function(s){
			var n=s.i;
			$rootScope.btrs1_city=n.region_name;
			$rootScope.back();
		}
	}
	catch (e) {
		showcatch(e);
	}
});
UserTutorCity.UserTutorCityData=function($q, $http,$stateParams,$rootScope){
	try {
		var data = [];
		var def = $q.defer();
		if(window.localStorage.u==undefined||window.localStorage.u==null||window.localStorage.u==""){			
			$rootScope.go("loginmain");
		}else{
			def.resolve(data);
		}
		
		return def.promise;
	} catch (e) {
		showcatch(e);
	}
}